<template>
  <div>
    <Card :bordered="false">
      <p slot="title">添加部门成员</p>
      <Form ref="form" :model="formData" :rules="ruleCustom" :label-width="70">
        <FormItem label="部门" prop="workspace_id">
          <label>
            <Select clearable transfer filterable v-model="formData.workspace_id" style="width: 300px">
              <Option v-for="item in workspaceData" :value="item.id" :key="item.name">{{ item.name }}</Option>
            </Select>
          </label>
        </FormItem>
        <FormItem label="成员" prop="user_id">
          <label>
            <Select clearable transfer filterable v-model="formData.user_id" style="width: 300px">
              <Option v-for="item in usersData" :value="item.user_id" :key="item.user_id">{{ item.name }}</Option>
            </Select>
          </label>
        </FormItem>
        <FormItem label="角色" prop="role_id">
          <label>
            <Select clearable transfer filterable v-model="formData.role_id" style="width: 300px">
              <Option v-for="item in roleData" :value="item.id" :key="item.id">{{ item.name }}</Option>
            </Select>
          </label>
        </FormItem>
        <Button
          type="primary"
          size="large"
          @click="handleSubmit('form')">添加
        </Button>
      </Form>
    </Card>
    <div slot="footer"/>
  </div>
</template>

<script>
  export default {
    name: 'AddWorkspaceUser',
    props: {
      showModal: {
        type: Boolean,
        default: false
      },
      handleType: {
        type: String,
        default: '编辑部门'
      },
      usersData: {
        type: Array,
        default() {
          return []
        }
      },
      roleData: {
        type: Array,
        default() {
          return []
        }
      },
      workspaceData: {
        type: Array,
        default() {
          return []
        }
      },
      formData: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    watch: {
      $route: {
        handler: function () {
          this.$refs['form'].resetFields()
        },
      }
    },
    data() {
      return {
        loading: true,
        ruleCustom: {
          workspace_id: [
            {required: true, message: '部门必填', trigger: 'blur', type: 'number'},
          ],
          user_id: [
            {required: true, message: '成员必填', trigger: 'blur', type: 'number'}
          ],
          role_id: [
            {required: true, message: '角色必填', trigger: 'blur', type: 'number'},
          ],
        }
      }
    },
    methods: {
      handleSubmit(name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            this.$emit('handleSubmit', this.formData)
          } else {
            return false
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>
